package net.wapproject.action;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import net.wapproject.bean.Category;
import net.wapproject.bean.Game;
import net.wapproject.bo.ICategoryBO;
import net.wapproject.bo.IGameBO;
import net.wapproject.bo.IUserBO;
import net.wapproject.utils.ImageSave;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.log4j.Logger;

public class BackendAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -406511548755428143L;
	private final Logger log = Logger.getLogger(BackendAction.class);
	private IUserBO userBo;
	private IGameBO gameBo;
	ICategoryBO categoryBo;
	// ---Customize---
	private List<Category> categoriesList;
	private String 		title;
	private String 		description;
	private String 		details;
	private String 		imageName;
	private File 		imageView;
	private String 		imageViewFileName;
	private String 		imageViewContentType;
	private File 		downloadFile;
	private String 		downloadFileFileName;
	private String 		downloadFileContentType;
	private File 		fileUpload;
	private String 		fileUploadContentType;
	private String 		fileUploadFileName;
	private Integer 	categoryId;
	private Category 	categoryEdit;
	private Integer 	categoryIdEdit;
	private String 		searchText;
	private Integer 	gameId;

	/**
	 * goto add news page
	 * @return
	 */
	public String gotoAddNewsPage() {
		String location = " gotoAddNewsPage() - ";
		log.debug(location + "BEGIN");
		//
		categoriesList = categoryBo.getParentCategory();

		log.debug(location + "END");
		return SUCCESS;
	}

	public String addNews() {
		String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
		String imageUrlSave = "/" + date + "/" + imageViewFileName;
		Category category = new Category();//categoryBo.getCategoryById(categoryId);
		Game game = new Game();
		
		game.setCategory(category);
		game.setCreateDate(new Date());
		game.setAvatar(imageUrlSave);
		game.setName(title);
		game.setDescription(StringEscapeUtils.unescapeHtml(description));
		game.setDetails(StringEscapeUtils.unescapeHtml(details));
		game.setRootCategory(category.getParentId());
		game.setTotalDownload(0);
		game.setTotalView(0);

		ImageSave.saveAndResize(imageView, imageUrlSave, 100, 100);
		
		try {
			String filePath = getRequest().getRealPath("upload/jar/");
			System.out.println("Server path:" + filePath);
			File fileToCreate = new File(filePath, fileUploadFileName);
			game.setPathDownload("upload/jar/" + fileUploadFileName);
			FileUtils.copyFile(fileUpload, fileToCreate);
		} catch (Exception ex) {
			log.error(ex);
		}
		gameBo.saveOrUpdate(game);
		return SUCCESS;
	}
	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDetails() {
		return details;
	}

	public void setDetails(String details) {
		this.details = details;
	}

	public String getImageName() {
		return imageName;
	}

	public void setImageName(String imageName) {
		this.imageName = imageName;
	}

	public File getImageView() {
		return imageView;
	}

	public void setImageView(File imageView) {
		this.imageView = imageView;
	}

	public String getImageViewFileName() {
		return imageViewFileName;
	}

	public void setImageViewFileName(String imageViewFileName) {
		this.imageViewFileName = imageViewFileName;
	}

	public String getImageViewContentType() {
		return imageViewContentType;
	}

	public void setImageViewContentType(String imageViewContentType) {
		this.imageViewContentType = imageViewContentType;
	}

	public File getDownloadFile() {
		return downloadFile;
	}

	public void setDownloadFile(File downloadFile) {
		this.downloadFile = downloadFile;
	}

	public String getDownloadFileFileName() {
		return downloadFileFileName;
	}

	public void setDownloadFileFileName(String downloadFileFileName) {
		this.downloadFileFileName = downloadFileFileName;
	}

	public String getDownloadFileContentType() {
		return downloadFileContentType;
	}

	public void setDownloadFileContentType(String downloadFileContentType) {
		this.downloadFileContentType = downloadFileContentType;
	}

	public File getFileUpload() {
		return fileUpload;
	}

	public void setFileUpload(File fileUpload) {
		this.fileUpload = fileUpload;
	}

	public String getFileUploadContentType() {
		return fileUploadContentType;
	}

	public void setFileUploadContentType(String fileUploadContentType) {
		this.fileUploadContentType = fileUploadContentType;
	}

	public String getFileUploadFileName() {
		return fileUploadFileName;
	}

	public void setFileUploadFileName(String fileUploadFileName) {
		this.fileUploadFileName = fileUploadFileName;
	}

	public Integer getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(Integer categoryId) {
		this.categoryId = categoryId;
	}

	public Category getCategoryEdit() {
		return categoryEdit;
	}

	public void setCategoryEdit(Category categoryEdit) {
		this.categoryEdit = categoryEdit;
	}

	public Integer getCategoryIdEdit() {
		return categoryIdEdit;
	}

	public void setCategoryIdEdit(Integer categoryIdEdit) {
		this.categoryIdEdit = categoryIdEdit;
	}

	public String getSearchText() {
		return searchText;
	}

	public void setSearchText(String searchText) {
		this.searchText = searchText;
	}

	public Integer getGameId() {
		return gameId;
	}

	public void setGameId(Integer gameId) {
		this.gameId = gameId;
	}

	public Logger getLog() {
		return log;
	}

	public List<Category> getCategoriesList() {
		return categoriesList;
	}

	public void setCategoriesList(List<Category> categoriesList) {
		this.categoriesList = categoriesList;
	}

	public IUserBO getUserBo() {
		return userBo;
	}

	public void setUserBo(IUserBO userBo) {
		this.userBo = userBo;
	}

	public ICategoryBO getCategoryBo() {
		return categoryBo;
	}

	public void setCategoryBo(ICategoryBO categoryBo) {
		this.categoryBo = categoryBo;
	}

	public IGameBO getGameBo() {
		return gameBo;
	}

	public void setGameBo(IGameBO gameBo) {
		this.gameBo = gameBo;
	}
}
